﻿using System;
using System.Data.SqlClient;
using System.Threading;
using System.Windows.Forms;
using com.fukefu.sql;

namespace com.fukefu
{
    public partial class LoginForm : BaseForm
    {
        public event MyDelegate0 Init;

        RegeditOperate reg = new RegeditOperate("qunfa");
        SqlServerOperate sql = SqlServerOperate.GetInstance();

        public LoginForm()
        {
            InitializeComponent();
            //加载所有分类
            SqlDataReader re = sql.Query("select * from master.dbo.sysdatabases where name like '%智能群发'");
            while (re.Read())
            {
                this.area.Items.Add(re["name"].ToString().Replace("智能群发", ""));
            }
            re.Close();
            /*查看是否保存密码到注册表*/
            if (reg.GetInt("issave") != -1)
            {
                uname.Text = reg.GetString("username");
                upass.Text = reg.GetString("password");
                area.Text = reg.GetString("area");
                checkBox1.Checked = true;
            }
        }
        //关闭
        private void CloseButton_Click(object sender, EventArgs e)
        {
            new ChangerPasswordForm().ShowDialog();
        }
        //登陆
        private void LoginButton_Click(object sender, EventArgs e)
        {
            LoginButton.Enabled = false;
            if (this.area.Text.Equals(""))
            {
                ShowMessageDialog("工作区域不能为空");
                LoginButton.Enabled = true;
                return;
            }
            new Thread(new ThreadStart(Login)).Start();
        }
        //登陆程序
        private void Login()
        {
            string name = GetControl("uname", ControlValue.Text).ToString();
            string pass = GetControl("upass", ControlValue.Text).ToString();
            string area = GetControl("area", ControlValue.Text).ToString();
            SqlDataReader re = sql.Query("select name,uid from 全局设置.dbo.用户 where name='" + name + "' and password='" + MyFunction.Md5(pass) + "' and area like '%" + area + "%'");
            if (re.Read())
            {
                LoginUser.Uid = re["uid"].ToString();
                LoginUser.Name = re["name"].ToString();
                Config.Sql_database = area + "智能群发";
                ShowMainForm();
                //保存
                if (Convert.ToBoolean(GetControl("checkBox1", ControlValue.Checked)) == true)
                {
                    reg.Set("username", this.GetControl("uname", ControlValue.Text).ToString());
                    reg.Set("password", this.GetControl("upass", ControlValue.Text).ToString());
                    reg.Set("area", this.GetControl("area", ControlValue.Text).ToString());
                    reg.Set("issave", 0);
                }
                else
                {
                    reg.Set("issave", -1);
                }
            }
            else
            {
                MessageBox.Show("登陆失败\t", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                SetControl("LoginButton", ControlValue.Enable, true);
            }
            re.Close();
        }
        //显示窗口
        private void ShowMainForm()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new MyDelegate0(ShowMainForm));
            }
            else
            {
                this.Hide();
                Init();
            }
        }

        private void UserLoginMainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }
    }
}
